package rabbit.flt.test.webflux.reactor;

import org.springframework.stereotype.Service;
import rabbit.flt.common.exception.FltException;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.concurrent.locks.LockSupport;

@Service
public class ReactorService {

    public Mono<Integer> reactorSleep(long mills) {
        return Mono.just(1).map(v -> {
            LockSupport.parkNanos(mills * 1000000);
            return v;
        });
    }

    public void normalSleep(long mills) {
        LockSupport.parkNanos(mills * 1000000);
    }

    /**
     * 连续调用
     *
     * @return
     */
    public Mono<Void> multiCall() {
        return reactorSleep(10).flatMap(v -> reactorSleep(10)).then();
    }

    public Mono<Void> multiFluxCall() {
        return Flux.just(1, 2).flatMap(v -> reactorSleep(10)).then();
    }

    public Mono<Void> monoError() {
        return Mono.error(new FltException("reactor error"));
    }

    public Flux<Void> fluxError() {
        return Flux.error(new FltException("reactor error"));
    }
}
